var WebPreview = {
    storageHandle: null,
    saveFilePathInfoArray: [],
    isOrNotLocalRecord: 0,
    isOrNotLocalSnapshot: 0,
    PtzTureType: {
        PTZ_TURN_UP: 1,
        PTZ_TURN_DOWN: 2,
        PTZ_TURN_LEFT: 3,
        PTZ_TURN_RIGHT: 4,
    },
    videoInterval: null,
    mediaHandle: null,
    firstGetRealTime: 0,
    isJumpPreview: false,
    playbackWinObj: null,
    startPlaybackTimeUTC: false,
    CallUserTalkWinObj: null,
    CallUserTalking: false,

    // 获取空闲的视频播放窗口
    GetFreeVideoWindow: function () {
        var keys = NPCCILY.windowContainers.keys();
        if (keys.length > 0) {
            for (var i = 0; i < keys.length; i++) {
                var ws = NPCCILY.windowContainers.get(keys[i]);
                if (!ws.active) {
                    return ws;
                }
            }
        }

        return null;
    },

    // 获取当前焦点的视频窗口
    GetFocusVideoWindow: function () {
        var keys = NPCCILY.windowContainers.keys();
        if (keys.length > 0) {
            for (var i = 0; i < keys.length; i++) {
                var key = keys[i];
                var windowContainerStruct = NPCCILY.windowContainers.get(keys[i]);
                if (windowContainerStruct.focus) {
                    return windowContainerStruct;
                }
            }
        }

        return null;
    },

    //点击播放回放视频
    ClickPlayVideoPlayback: function(resHandle) {
        var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
        var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, resHandle).value;
        if (resType == NPCCEnum.ResType.RES_TYPE_CAM && bOnline == 0) {
            alert('播放视频失败，设备不在线');
            return;
        }

        if (resType == NPCCEnum.ResType.RES_TYPE_CAM && bOnline == 1) {
            try {
                if (WebPreview.CheckCameraResIsCanPlay(resHandle)) {
                    var windowContainerStruct = NPCCILY.windowContainers.get('videoPlayback');
                    if (windowContainerStruct.window) {
                        alert('启动视频失败，没有空闲的视频窗口');
                        return false;
                    }
                    else {
                        WebPreview.createVideoWindow('videoPlayback');
                    }

                    setTimeout(function () {
                        var winObj = windowContainerStruct.window;
                        if (winObj) {
                            // 开始预览视频
                            winObj.params.puid = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, resHandle).value;
                            winObj.params.resIdx = NPCCIF.COMMON_GetResIdx(NPCCILY.Plug.ccPlugin, resHandle).value;
                            winObj.params.cameraResHandle = resHandle;
                            var operator = NPCCILY.StartPreview(winObj, NPCCEnum.StopRealtimePreviewType.SREP_VIDEO, true);

                            console.log('打开视频：' + operator.errorCode);
                            if (operator.errorCode == ErrorCode.NP_E_OK) {
                                // 表示这个打开的视频是否支持回放的功能
                                var isSupportedRealtimePlayback = NPCCIF.RSTW_IsSupportedRealtimePlayback(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle);
                                // console.log(isSupportedRealtimePlayback.value);
                                if (isSupportedRealtimePlayback.errorCode == ErrorCode.NP_E_OK) {
                                    if (isSupportedRealtimePlayback.value == '1') {
                                        //开始实时时间
                                        $('.startRealtimeUTC').html(WebPreview.CalcUTCTime());
                                        WebPreview.mediaHandle = winObj.params.mediaHandle;
                                        $('a.videoPreview_progress').slider('option', 'max', 300);
                                        var videoInterval = function() {
                                            // 实时时间
                                            // 获取当前音视频帧的时间戳
                                            var realtimeTime = NPCCIF.RSTW_GetRealtimeTimestamp(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle).value;
                                            if (!Number(WebPreview.firstGetRealTime)) {
                                                WebPreview.firstGetRealTime = realtimeTime;
                                                console.log('WebPreview.firstGetRealTime：' + WebPreview.firstGetRealTime);
                                            }
                                            var realtimeTimeProgress = parseInt((realtimeTime - WebPreview.firstGetRealTime) / 1000);
                                            // 实时预览总时长
                                            $('.realtime').html(WebPreview.CalcTime(realtimeTimeProgress));
                                            $('.realtime').attr("href", realtimeTimeProgress);
                                            var realtimeUTC = NPCCIF.RSTW_GetRealtimeTime(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle).value;
                                            // 实时时间
                                            $('.realtimeUTC').html(WebPreview.CalcUTCTime(realtimeUTC * 1000));

                                            // 回放时间
                                            // 获取正在回放的音视频的时间戳
                                            if (WebPreview.isJumpPreview) {
                                                var playbackTime = NPCCIF.RSTW_GetRealPlaybackTimestamp(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle).value;
                                                // console.log('回放时长：' + playbackTime);
                                                if (playbackTime != '0') {
                                                    var playbackTimeProgress = parseInt((playbackTime - WebPreview.firstGetRealTime) / 1000);
                                                    // 回放时长
                                                    $('.playbackTime').html(WebPreview.CalcTime(playbackTimeProgress));
                                                    var playbackTimeUTC = NPCCIF.RSTW_GetRealPlaybackTime(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle).value;
                                                    // console.log('playbackTimeUTC：' + playbackTimeUTC);
                                                    // 回放时间
                                                    $('.playbackTimeUTC').html(WebPreview.CalcUTCTime(playbackTimeUTC * 1000));
                                                    if (WebPreview.startPlaybackTimeUTC) {
                                                        //开始回放时间
                                                        $('.startPlaybackTimeUTC').html(WebPreview.CalcUTCTime(playbackTimeUTC * 1000));
                                                        WebPreview.startPlaybackTimeUTC = false;
                                                    }
                                                }
                                            }

                                            // 进度条
                                            if (!WebPreview.isJumpPreview) {
                                                if (realtimeTimeProgress <= 300) {
                                                    $('a.videoPreview_progress').slider('option', 'value', realtimeTimeProgress);
                                                }
                                                else {
                                                    $('a.videoPreview_progress').slider('option', 'value', realtimeTimeProgress);
                                                }
                                            }
                                            else {
                                                if (playbackTimeProgress <= 300) {
                                                    $('a.videoPreview_progress').slider('option', 'value', playbackTimeProgress);
                                                }
                                                else {
                                                    $('a.videoPreview_progress').slider('option', 'max', realtimeTimeProgress);
                                                }
                                            }

                                            // 如果预览时间超过5分钟
                                            if (realtimeTimeProgress > 300) {
                                                $('a.videoPreview_progress').slider('option', 'min', realtimeTimeProgress - 300);
                                                $('a.videoPreview_progress').slider('option', 'max', realtimeTimeProgress);
                                                $('.startTime').html(WebPreview.CalcTime((realtimeTimeProgress - 300)));
                                                $('.endTime').html(WebPreview.CalcTime(realtimeTimeProgress));
                                            }
                                        }
                                        WebPreview.videoInterval = setInterval(videoInterval, 1000);
                                    }
                                    else {
                                        console.log('打开的视频不支持回放的功能');
                                    }

                                }

                                operator = NPCCILY.CAM_GetDefinition(winObj);
                                if (operator.errorCode == ErrorCode.NP_E_OK) {
                                    $('#def').val(operator.value);
                                }
                                else {
                                    console.log('获取清晰度失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                }
                            }
                            else {
                                alert('开启视频失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                return false;
                            }

                            WebPreview.ClickVideoWindow(winObj);
                            windowContainerStruct.active = true;
                            return true;
                        }
                        else {
                            alert('开启视频失败，视频播放窗口还没有创建');
                            return false;
                        }
                    }, 0)
                }
            }
            catch (e) {
                alert('启动视频异常：' + e.name + ', ' + e.message);
                return false;
            }
            return;
        }
    },

    // 点击播放视频
    ClickCameraRes: function (resHandle) {
        var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
        var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, resHandle).value;
        if (resType == NPCCEnum.ResType.RES_TYPE_CAM && bOnline == 0) {
            alert('播放视频失败，设备不在线');
            return;
        }

        if (resType == NPCCEnum.ResType.RES_TYPE_CAM && bOnline == 1) {
            WebPreview.PlayVideo(resHandle);
            return;
        }
    },

    // 启动视频
    PlayVideo: function (resHandle) {
        try {
            if (WebPreview.CheckCameraResIsCanPlay(resHandle)) {
                var windowContainerStructFree = WebPreview.GetFreeVideoWindow();
                var windowContainerStructFocus = WebPreview.GetFocusVideoWindow();
                var windowContainerStruct;
                // 如果没有获取焦点，找空闲窗口
                if (windowContainerStructFree && !windowContainerStructFocus) {
                    windowContainerStruct = windowContainerStructFree;
                }
                else if (windowContainerStructFree && windowContainerStructFocus) {
                    // 如果获取焦点的窗口已经播放视频了，找下一个空闲窗口
                    if (windowContainerStructFocus.active) {
                        windowContainerStruct = windowContainerStructFree;
                    }
                    else {
                        // 获取焦点但没有播放视频
                        windowContainerStruct = windowContainerStructFocus;
                    }
                }

                WebClient.CreateVideoWindow(windowContainerStruct);

                setTimeout(function () {
                    if (windowContainerStruct) {
                        var winObj = windowContainerStruct.window;
                        if (winObj) {
                            // 开始预览视频
                            winObj.params.puid = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, resHandle).value;
                            winObj.params.resIdx = NPCCIF.COMMON_GetResIdx(NPCCILY.Plug.ccPlugin, resHandle).value;
                            winObj.params.cameraResHandle = resHandle;
                            var operator = NPCCILY.StartPreview(winObj, NPCCEnum.StopRealtimePreviewType.SREP_VIDEO, false);
                            console.log('打开视频：' + operator.errorCode);
                            if (operator.errorCode == ErrorCode.NP_E_OK) {
                                operator = NPCCILY.CAM_GetDefinition(winObj);
                                if (operator.errorCode == ErrorCode.NP_E_OK) {
                                    $('#def').val(operator.value);
                                }
                                else {
                                    console.log('获取清晰度失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                }
                            }
                            else {
                              console.log(ErrorCode.Error2Desc(operator.errorCode))
                                alert('开启视频失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                return false;
                            }

                            WebPreview.ClickVideoWindow(winObj);
                            windowContainerStruct.active = true;
                            return true;
                        }
                        else {
                            alert('开启视频失败，视频播放窗口还没有创建');
                            return false;
                        }
                    }
                    else {
                        alert('启动视频失败，没有空闲的视频窗口');
                        return false;
                    }
                }, 0)

            }
        }
        catch (e) {
            alert('启动视频异常：' + e.name + ', ' + e.message);
            return false;
        }
    },

    // 计算时间 00:00
    CalcTime: function(time) {
        var timeRes = '00:00';
        if (time < 10) {
            timeRes = '00:0' + time;
        }
        else if (time >= 10 && time < 60) {
            timeRes = '00:' + time;
        }
        else if (time >= 60 && time < 600) {
            timeRes = '0' + Math.floor(time/60) + ':' + ((time%60) > 9 ? (time%60) : '0' + (time%60));
        }
        else if (time >= 600) {
            timeRes =  Math.floor(time/60) + ':' + ((time%60) > 9 ? (time%60) : '0' + (time%60));
        }

        return timeRes;
    },

    // 计算UTC时间
    CalcUTCTime: function(UTCDateString) {
        function formatFunc(str) {    //格式化显示
            return str > 9 ? str : '0' + str
        }

        if (UTCDateString) {
            var date2 = new Date(UTCDateString);
        }
        else {
            var date2 = new Date();
        }

        var year = date2.getFullYear();
        var mon = formatFunc(date2.getMonth() + 1);
        var day = formatFunc(date2.getDate());
        var hour = formatFunc(date2.getHours());
        var min = formatFunc(date2.getMinutes());
        var sec = formatFunc(date2.getSeconds());
        var dateStr = year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec;
        return dateStr;
    },

    // 检测指定摄像头资源是否可以播放
    CheckCameraResIsCanPlay: function (resHandle) {
        var keys = NPCCILY.windowContainers.keys();
        if (keys.length > 0) {
            for (var i = 0; i < keys.length; i++) {
                var ws = NPCCILY.windowContainers.get(keys[i]);
                if (ws.active) {
                    var winObj = ws.window;
                    if (winObj.status.playvideoing) {
                        var puid = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, resHandle).value;
                        var resIdx = NPCCIF.COMMON_GetResIdx(NPCCILY.Plug.ccPlugin, resHandle).value;
                        if (winObj.params.puid == puid && winObj.params.resIdx == resIdx) {
                            alert('视频已经在播放了');
                            return false;
                        }
                    }
                }
            }
        }

        return true;
    },

    // 点击视频窗口
    ClickVideoWindow: function (window) {
        var keys = NPCCILY.windowContainers.keys();
        if (keys.length > 0) {
            for (var i = 0; i < keys.length; i++) {
                var key = keys[i];
                var windowContainerStruct = NPCCILY.windowContainers.get(keys[i]);
                if (windowContainerStruct && windowContainerStruct.window) {
                    if (windowContainerStruct.window == window) {
                        windowContainerStruct.focus = true;
                        if (windowContainerStruct.window.status.playvideoing) {
                            $(".closeView").attr("disabled", false);
                            $(".audio").attr("disabled", false);
                            $(".talk").attr("disabled", false);
                            $(".location").attr("disabled", false);
                            $(".top").attr("disabled", false);
                            $(".bottom").attr("disabled", false);
                            $(".left").attr("disabled", false);
                            $(".right").attr("disabled", false);
                            $(".snapShot").attr("disabled", false);
                            $(".record").attr("disabled", false);
                            $(".callUserTalk").attr("disabled", false);
                            $(".platformStorage").attr("disabled", false);

                            $(".definition").attr("disabled", false);
                            $(".volume").attr("disabled", false);

                            var operator = NPCCILY.CAM_GetDefinition(windowContainerStruct.window);
                            if (operator.errorCode == ErrorCode.NP_E_OK) {
                                $('#def').val(operator.value);
                            }
                            else {
                                console.log('获取清晰度失败：' + ErrorCode.Error2Desc(operator.errorCode));
                            }

                            if (windowContainerStruct.window.status.playaudioing) {
                                $('.audio').val('停止音频');
                                operator = NPCCILY.AUD_GetVolume(windowContainerStruct.window);
                                if (operator.errorCode == ErrorCode.NP_E_OK) {
                                    $('#vol').val(operator.value);
                                }
                                else {
                                    alert('获取音频音量失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                }
                            }
                            else {
                                $('.audio').val('音频');
                                $('#vol').val('');
                            }

                            if (windowContainerStruct.window.status.playtalking) {
                                $('.talk').val('停止对讲');
                            }
                            else {
                                $('.talk').val('对讲');
                            }

                            // 语音通话
                            if (windowContainerStruct.window.status.playtalking && WebPreview.CallUserTalking) {
                                $('.callUserTalk').val('停止对讲');
                            }
                            else {
                                $('.callUserTalk').val('语音通话');
                            }

                            if (windowContainerStruct.window.status.playlocation) {
                                $('.location').val('停止定位');
                            }
                            else {
                                $('.location').val('定位');
                            }

                            if (windowContainerStruct.window.status.playrecording) {
                                $('.record').val('停止录像');
                            }
                            else {
                                $('.record').val('本地录像');
                            }

                            if (windowContainerStruct.window.status.playstorage) {
                                $('.platformStorage').val('手动停止平台录像');
                            }
                            else {
                                $('.platformStorage').val('手动开启平台录像');
                            }

                        }
                        else {
                            $(".audio").attr("disabled", true);
                            $('.audio').val('音频');
                            $(".talk").attr("disabled", true);
                            $('.talk').val('对讲');
                            $(".location").attr("disabled", true);
                            $('.location').val('定位');
                            $(".top").attr("disabled", true);
                            $(".bottom").attr("disabled", true);
                            $(".left").attr("disabled", true);
                            $(".right").attr("disabled", true);
                            $(".snapShot").attr("disabled", true);
                            $(".record").attr("disabled", true);
                            $(".record").val('本地录像');
                            $(".callUserTalk").attr("disabled", true);
                            $('.callUserTalk').val('语音通话');
                            $(".platformStorage").attr("disabled", true);
                            $(".platformStorage").val('手动开启平台录像');

                            $('#def').val('');
                            $(".definition").attr("disabled", true);
                            $('#vol').val('');
                            $(".volume").attr("disabled", true);
                        }

                        // $('#'+ key).css("border", "1px solid red");
                        $('div.video').removeClass("videoWnd_selected").addClass("videoWnd_border");
                        $('#'+ key).addClass("videoWnd_selected").removeClass("videoWnd_border");
                    }
                    else {
                        windowContainerStruct.focus = false;
                        $('#'+ key).addClass("videoWnd_border");
                    }
                }
                else {
                    windowContainerStruct.focus = false;
                }
            }
        }
    },

    // 关闭视频
    StopVideo: function () {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct) {
                return WebPreview.StopVideoByWindowObj(windowContainerStruct);
            }
            return false;
        }
        catch (e) {
            alert('停止失败异常: ' + e.name + ', ' + e.message);
            return false;
        }
    },

    // 关闭指定窗口的视频
    StopVideoByWindowObj: function (windowContainerStruct, pauseOrClose) {
        var winObj = windowContainerStruct.window;
        if (winObj) {
            if (winObj.status.playaudioing) {
                if (!WebPreview.PlayOrStopAudio(winObj)) {
                    return false;
                }
            }

            if (winObj.status.playtalking) {
                if (!WebPreview.PlayOrStopTalk(winObj)) {
                    return false;
                }
            }

            // 录像
            if (winObj.status.playrecording) {
                if (!WebPreview.LocalRecord()) {
                    return false;
                }
            }

            if (winObj.status.playlocation) {
                if (!WebPreview.PlayOrStopLocation(winObj)) {
                    return false;
                }
            }

            //平台录像
            if (winObj.status.playstorage) {
                if (!WebPreview.ManualStartOrStopStorage()) {
                    return false;
                }
            }

            if (winObj.status.playvideoing) {
                if (pauseOrClose) {
                    // 切换视频功能：不关闭视频，将播放窗口的窗口句柄设置为空
                    var operator = NPCCIF.SetRenderWnd(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle, '');
                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                        delete windowContainerStruct.window;
                        $('#' + winObj.pwPluginName).remove();

                        NPCCIF.RefreshImage(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle);
                        windowContainerStruct.active = false;
                        windowContainerStruct.focus = true;
                    }
                }
                else {
                    var operator = NPCCILY.StopPreview(winObj, NPCCEnum.StopRealtimePreviewType.SREP_VIDEO);
                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                        if (winObj.containerId == 'videoPlayback') {
                            clearInterval(WebPreview.videoInterval);
                            WebPreview.videoInterval = null;
                            WebPreview.firstGetRealTime = 0;
                            $('.startTime').html('0:00');
                            $('.endTime').html('5:00');
                            $('a.videoPreview_progress').slider('option', 'value', 0);
                            $('.playbackTime').html(''); // 回放时长
                            $('.playbackTimeUTC').html(''); // 回放时间

                            $('.realtime').html(''); // 实时预览总时长
                            $('.realtimeUTC').html(''); // 实时时间
                            $('.startRealtimeUTC').html('');//开始实时时间
                            $('.startPlaybackTimeUTC').html('');//开始回放时间
                        }

                        $('#def').val('');
                        $('#vol').val('');
                        delete windowContainerStruct.window;
                        $('#' + winObj.pwPluginName).remove();
                        WebClient.setFocus(winObj.containerId);
                        NPCCIF.RefreshImage(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle);
                        winObj.params.cameraResHandle = null;
                        windowContainerStruct.active = false;
                        windowContainerStruct.focus = true;

                    }
                    else {
                        alert('停止视频失败：' + ErrorCode.Error2Desc(operator.errorCode));
                        return false;
                    }
                }

            }
            return true;
        }
        return false;
    },

    // 音频
    ClickAudioRes: function () {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    // 获取当前在线设备句柄
                    var onlines = onlineDevice();
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var bFind = false;
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_AUD);
                            if (resHandle) {
                                windowContainerStruct.window.params.audioResHandle = resHandle;
                                bFind = true;
                            }

                            if (bFind) {
                                WebPreview.PlayOrStopAudio(windowContainerStruct.window);
                            }
                            else {
                                alert('没有音量资源');
                            }
                            return;
                        }
                    }
                }
                else {
                    alert('请先选择一个视频播放');
                }
            }
        }
        catch (e) {
            alert('开启或停止音频异常：' + e.name + '::' + e.message);
        }
    },

    // 启动/关闭音频
    PlayOrStopAudio: function (winObj) {
        if (winObj.status.playaudioing) {
            var operator = NPCCILY.StopPreview(winObj, NPCCEnum.StopRealtimePreviewType.SREP_AUDIO);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                $('#vol').val('');
                $('.audio').val('音频');
                winObj.params.audioResHandle = null;
            }
            else {
                alert('停止音频失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }
        else {
            if (winObj.status.playtalking) {
                if (!WebPreview.PlayOrStopTalk(winObj)) {
                    winObj.params.audioResHandle = null;
                    return false;
                }
            }

            var operator = NPCCILY.StartPreview(winObj, NPCCEnum.StopRealtimePreviewType.SREP_AUDIO);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                operator = NPCCILY.AUD_GetVolume(winObj);
                if (operator.errorCode == ErrorCode.NP_E_OK) {
                    $('#vol').val(operator.value);
                    // alert('获取音频音量成功');
                }
                else {
                    alert('获取音频音量失败：' + ErrorCode.Error2Desc(operator.errorCode));
                }

                $('.audio').val('停止音频');
            }
            else {
                winObj.params.audioResHandle = null;
                alert('开启音频失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }
        return true;
    },

    // 对讲
    ClickTalkbackRes: function () {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    // 获取当前在线设备句柄
                    var onlines = onlineDevice();
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var bFind = false;
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_TBK);
                            if (resHandle) {
                                windowContainerStruct.window.params.talkbackResHandle = resHandle;
                                bFind = true;
                            }

                            if (bFind) {
                                WebPreview.PlayOrStopTalk(windowContainerStruct.window);
                            }
                            else {
                                alert('没有对讲资源');
                            }

                            return;
                        }
                    }
                }
                else {
                    alert('请先选择一个视频播放');
                }
            }
        }
        catch (e) {
            alert('开启或停止对讲异常：' + e.name + '::' + e.message);
        }
    },

    // 启动和关闭对讲
    PlayOrStopTalk: function (winObj) {
        if (winObj.status.playtalking) {
            var operator = NPCCILY.StopTalk(winObj);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                $('.talk').val('对讲');
                winObj.params.talkbackResHandle = null;
            }
            else {
                alert('停止对讲失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }
        else {
            if (winObj.status.playaudioing) {
                if (!WebPreview.PlayOrStopAudio(winObj)) {
                    winObj.params.talkbackResHandle = null;
                    return false;
                }

                if (!WebPreview.PlayOrStopCallUserTalk(winObj)) {
                    winObj.params.talkbackResHandle = null;
                    return false;
                }
            }

            var operator = NPCCILY.StartTalk(winObj);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                $('.talk').val('停止对讲');
            }
            else {
                winObj.params.talkbackResHandle = null;
                alert('开启对讲失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }
        return true;
    },

    // 语音通话
    CallUserTalk: function() {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    var puid = windowContainerStruct.window.params.puid;
                    var onlines = onlineDevice();
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var bFind = false;
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_TBK);
                            if (resHandle) {
                                windowContainerStruct.window.params.talkbackResHandle = resHandle;
                                bFind = true;
                            }

                            if (bFind) {
                                WebPreview.PlayOrStopCallUserTalk(windowContainerStruct.window);
                            }
                            else {
                                alert('没有对讲资源');
                            }

                            return;
                        }
                    }
                }
            }
            else {
                alert('请先选择一个视频播放');
            }
        }
        catch (e) {
            alert('开启语音通话异常：' + e.name + '::' + e.message);
        }
    },

    //启动或关闭语音通话
    PlayOrStopCallUserTalk: function(winObj) {
        if (winObj.status.playtalking && $('.callUserTalk').val() == '停止对讲') {
            var operator = NPCCILY.StopTalk(winObj);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                $('.callUserTalk').val('语音通话');
                winObj.params.talkbackResHandle = null;
                WebPreview.CallUserTalking = false;
            }
            else {
                alert('停止对讲失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }
        else if (!winObj.status.playtalking && $('.callUserTalk').val() == '语音通话') {
            var operator = NPCCIF.RSTW_EVT_CallUserTalkRequest(NPCCILY.Plug.ccPlugin, WebClient.hServer, winObj.params.puid, '');
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                console.log('呼叫用户语音通话的请求发送成功');
                WebPreview.CallUserTalkWinObj = winObj;
            }
            else {
                console.log('呼叫用户语音通话的请求发送失败');
            }
        }

        return true;
    },

    // 定位
    ClickLocationRes: function () {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    // 获取当前在线设备句柄
                    var onlines = onlineDevice();
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var bFind = false;
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_LOC);
                            if (resHandle) {
                                windowContainerStruct.window.params.locationResHandle = resHandle;
                                bFind = true;
                            }

                            if (bFind) {
                                WebPreview.PlayOrStopLocation(windowContainerStruct.window);
                            }
                            else {
                                alert('没有定位资源');
                            }

                            return;
                        }
                    }
                }
                else {
                    alert('请先选择一个视频播放');
                }
            }
        }
        catch (e) {
            alert('开启或停止定位异常：' + e.name + '::' + e.message);
        }
    },

    // 启动和关闭定位
    PlayOrStopLocation: function (winObj) {
        if (winObj.status.playlocation) {
            var operator = NPCCILY.StopLocation(winObj);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                $('.location').val('定位');
                $('#location_info').val('');
                winObj.params.locationResHandle = null;
            }
            else {
                alert('停止定位失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }
        else {
            var operator = NPCCILY.StartLocation(winObj);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                $('.location').val('停止定位');
            }
            else {
                winObj.params.locationResHandle = null;
                alert('开启定位失败：' + ErrorCode.Error2Desc(operator.errorCode));
                return false;
            }
        }

        return true;
    },

    // 启动云台移动
    StartTurePtz: function (ptzTureType) {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    //获取所有设备句柄
                    var onlines = onlineDevice();
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var bFind = false;
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_PTZ);
                            if (resHandle) {
                                windowContainerStruct.window.params.ptzResHandle = resHandle;
                                bFind = true;
                            }

                            if (bFind) {
                                if (ptzTureType == WebPreview.PtzTureType.PTZ_TURN_UP) {
                                    var operator = NPCCILY.PTZ_StartTurnUp(windowContainerStruct.window);
                                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                                        // alert('开启云台向上移动成功');
                                    }
                                    else {
                                        alert('开启云台向上移动失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                    }
                                }
                                else if (ptzTureType == WebPreview.PtzTureType.PTZ_TURN_DOWN) {
                                    var operator = NPCCILY.PTZ_StartTurnDown(windowContainerStruct.window);
                                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                                        // alert('开启云台向下移动成功');
                                    }
                                    else {
                                        alert('开启云台向下移动失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                    }
                                }
                                else if (ptzTureType == WebPreview.PtzTureType.PTZ_TURN_LEFT) {
                                    var operator = NPCCILY.PTZ_StartTurnLeft(windowContainerStruct.window);
                                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                                        // alert('开启云台向左移动成功');
                                    }
                                    else {
                                        alert('开启云台向左移动失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                        return false;
                                    }
                                }
                                else if (ptzTureType == WebPreview.PtzTureType.PTZ_TURN_RIGHT) {
                                    var operator = NPCCILY.PTZ_StartTurnRight(windowContainerStruct.window);
                                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                                        // alert('开启云台向右移动成功');
                                    }
                                    else {
                                        alert('开启云台向右移动失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                        return false;
                                    }
                                }
                                else {
                                    // console.log('传入的云台类型参数不存在');
                                }
                            }
                            else {
                                alert('该设备没有云台资源');
                            }

                            return;
                        }
                    }
                }
                else {
                    alert('请先选择一个视频播放');
                }
            }
        }
        catch (e) {
            alert('启动云台移动异常：' + e.name + '::' + e.message);
        }
    },

    // 停止云台移动
    StopTurePtz: function () {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.params.ptzResHandle) {
                    var operator = NPCCILY.PTZ_StopTurn(windowContainerStruct.window);
                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                        // alert('停止云台移动成功');
                    }
                    else {
                        alert('停止云台移动失败：' + ErrorCode.Error2Desc(operator.errorCode));
                    }
                }
            }
        }
        catch (e) {
            alert('停止云台移动异常: ' + e.name + ', ' + e.message);
        }
    },

    // 设置清晰度
    SetDefinition: function() {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    var onlines = onlineDevice();
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_CAM);
                            if (resHandle) {
                                windowContainerStruct.window.params.cameraResHandle = resHandle;
                                var definition = $('#def').val();
                                var operator = NPCCILY.CAM_SetDefinition(windowContainerStruct.window, definition);
                                if (operator.errorCode == ErrorCode.NP_E_OK) {
                                    alert('设置清晰度成功');
                                }
                                else {
                                    alert('设置清晰度失败：' + ErrorCode.Error2Desc(operator.errorCode));
                                    $('#def').val('');
                                    return false;
                                }
                            }

                            return;
                        }
                    }
                }
                else {
                    $('#def').val('');
                    alert('请先选择一个视频播放');
                }
            }
        }
        catch (e) {
            alert('设置清晰度异常：' + e.name + '::' + e.message);
        }
    },

    // 设置音频音量
    SetAudVolume: function() {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    var onlines = onlineDevice();
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_AUD);
                            if (resHandle) {
                                windowContainerStruct.window.params.audioResHandle = resHandle;
                            }
                        }
                    }

                    var volume = $('#vol').val();
                    var operator = NPCCILY.AUD_SetVolume(windowContainerStruct.window, volume);
                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                        alert('设置音频音量成功');
                    }
                    else {
                        $('#vol').val('');
                        alert('设置音频音量失败：' + ErrorCode.Error2Desc(operator.errorCode));
                    }
                }
                else {
                    $('#vol').val('');
                    alert('请先选择一个视频播放');
                }
            }
        }
        catch (e) {
            alert('设置音频音量异常：' + e.name + '::' + e.message);
        }
    },

    // 本地录像或停止录像
    LocalRecord: function() {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    var winObj = windowContainerStruct.window;
                    if (winObj.status.playrecording) {
                        var operator = NPCCILY.StopLocalRecord(windowContainerStruct.window); // 默认存放在C盘
                        if (operator.errorCode == ErrorCode.NP_E_OK) {
                           console.log('录像成功');
                            $('.record').val('本地录像');
                            var puid = windowContainerStruct.window.params.puid;
                            var endTime = NPCCUtils.DateFormat('HH#mm#ss').replace(/#/g, '');
                            for (var j = 0; j < WebPreview.saveFilePathInfoArray.length; j++) {
                                if (WebPreview.saveFilePathInfoArray[j].puid == puid) {
                                    var array = WebPreview.saveFilePathInfoArray[j].saveAsFilePathOld.split('.');
                                    var lastInfo = array[0] + '_' + endTime;
                                    array[0] = lastInfo;
                                    var saveAsFilePathNew = array.join('.');
                                    // 修改文件名
                                    operator = NPCCIF.ReFileName(NPCCILY.Plug.ccPlugin, NPCCUtils.encodeUnicode(
                                        NPCCUtils.UnicodetoUTF8(WebPreview.saveFilePathInfoArray[j].saveAsFilePathOld)), NPCCUtils.encodeUnicode(
                                        NPCCUtils.UnicodetoUTF8(saveAsFilePathNew)));
                                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                                        WebPreview.saveFilePathInfoArray.splice(j, 1)
                                    }
                                }
                            }
                            WebPreview.isOrNotLocalRecord = 1;
                        }
                        else {
                            alert('停止录像失败：' + ErrorCode.Error2Desc(operator.errorCode));
                            return false;
                        }
                    }
                    else {
                        var onlines = onlineDevice();
                        var euipName = null;
                        var puid = windowContainerStruct.window.params.puid;
                        for (var i = 0; i < onlines.length; i++) {
                            var devHandle = onlines[i];
                            var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                            if (puidGet == puid) {
                                euipName = NPCCIF.COMMON_GetName(NPCCILY.Plug.ccPlugin, devHandle).value;
                                var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_CAM);
                                if (resHandle) {
                                    windowContainerStruct.window.params.cameraResHandle = resHandle;
                                }
                            }
                        }

                        // 获取摄像头名称
                        var operator = NPCCIF.COMMON_GetName(NPCCILY.Plug.ccPlugin, windowContainerStruct.window.params.cameraResHandle);
                        console.log(operator);

                        var date = new Date();
                        var month = date.getMonth() + 1;
                        month = month < 10 ? '0' + month : '' + month;
                        var yearMonth = (date.getFullYear() + '').slice(2, 4).concat(month);
                        var day = date.getDate();
                        day = day < 10 ? '0' + day : '' + day;
                        var saveFileDir="C:/RST_Client/Storage/" + yearMonth + "/" + day + "/" + euipName+ '_' + NPCCUtils.DateFormat("yyyy#MM#dd#_#HH#mm#ss#_#l").replace(/#/g, "");
                        var uiMaxRecordTimeInterval = 15 * 60 * 1000; // 15分钟
                        var saveFileName = operator.value + '_' + NPCCUtils.DateFormat("yyyy#MM#dd#_#HH#mm#ss").replace(/#/g, "") + ".rav";
                        // var saveAsFilePath = saveFileDir + "\\" + saveFileName;
                        var saveAsFilePathOld = saveFileDir + '\\' + saveFileName;
                        var filePathInfoObj = {};
                        filePathInfoObj['puid'] = puid;
                        filePathInfoObj['saveAsFilePathOld'] = saveAsFilePathOld;
                        WebPreview.saveFilePathInfoArray.push(filePathInfoObj);

                        var operator = NPCCILY.StartLocalRecord(windowContainerStruct.window, NPCCUtils.encodeUnicode(NPCCUtils.UnicodetoUTF8(saveAsFilePathOld)), uiMaxRecordTimeInterval); // 默认存放在C盘
                        if (operator.errorCode == ErrorCode.NP_E_OK) {
                            console.log('本地开始录像，成功');
                            $('.record').val('停止录像');
                        }
                        else {
                            alert('本地录像失败：' + ErrorCode.Error2Desc(operator.errorCode));
                            return false;
                        }

                    }

                    return true;
                }
                else {
                    // alert('请先选择一个视频播放');
                }

            }
        }
        catch (e) {
            // alert("excep error = " + e.name + "::" + e.message);
            // return false;
            // $('.warnTips').html('开启录像或停止录像异常：' + e.name + '::' + e.message);
            // WebClient.showOrHide();
        }
    },

    // 本地抓拍
    LocalSnapshot: function() {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    // 获取摄像头名称
                    var operator = NPCCIF.COMMON_GetName(NPCCILY.Plug.ccPlugin, windowContainerStruct.window.params.cameraResHandle);
                    console.log(operator);
                    var onlines = onlineDevice();
                    var euipName = null;
                    var puid = windowContainerStruct.window.params.puid;
                    for (var i = 0; i < onlines.length; i++) {
                        var devHandle = onlines[i];
                        var puidGet = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
                        if (puidGet == puid) {
                            euipName = NPCCIF.COMMON_GetName(NPCCILY.Plug.ccPlugin, devHandle).value;
                            var resHandle = getSubRes(devHandle, NPCCEnum.ResType.RES_TYPE_CAM);
                            if (resHandle) {
                                windowContainerStruct.window.params.cameraResHandle = resHandle;
                            }
                        }
                    }

                    var date = new Date();
                    var month = date.getMonth() + 1;
                    month = month < 10 ? '0' + month : '' + month;
                    var yearMonth = (date.getFullYear() + '').slice(2, 4).concat(month);
                    var day = date.getDate();
                    day = day < 10 ? '0' + day : '' + day;
                    var saveFileDir="C:/RST_Client/Storage/" + yearMonth + "/" + day + "/" + euipName+ '_' + NPCCUtils.DateFormat("yyyy#MM#dd#_#HH#mm#ss#_#l").replace(/#/g, "");
                    var saveFileName = operator.value + '_' + NPCCUtils.DateFormat("yyyy#MM#dd#_#HH#mm#ss#_#l").replace(/#/g, "")+ ".bmp";
                    var saveAsFilePath = saveFileDir + "/" + saveFileName;
                    var operator = NPCCILY.Snapshot(windowContainerStruct.window, NPCCUtils.encodeUnicode(NPCCUtils.UnicodetoUTF8(saveAsFilePath)));
                    if (operator.errorCode == ErrorCode.NP_E_OK) {
                        WebPreview.isOrNotLocalSnapshot = 1;
                        alert('本地抓拍成功');
                    }
                    else {
                        alert('本地抓拍失败');

                        return false;
                    }
                }
            }
        }
        catch (e) {
            alert('本地抓拍异常：' + e.name + '::' + e.message);
        }
    },

    // 打开全屏
    ShowFullWindow: function (notify) {
        try {
            if (notify) {
                NPCCILY.windowContainers.each(function (item) {
                    var windowContainerStruct = item.value;
                    var winObj = windowContainerStruct.window;
                    if (winObj == notify.handle && winObj.status.playvideoing) {
                        var fullWindow = notify.keyData;
                        NPCCIF.SetRenderWnd(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle, fullWindow);
                    }
                });

                WebQuery.windowContainers.each(function(item) {
                    var windowContainerStruct = item.value;
                    var winObj = windowContainerStruct.window;
                    if (winObj == notify.handle && winObj.status.playvideoing) {
                        var fullWindow = notify.keyData;
                        NPCCIF.SetRenderWnd(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle, fullWindow);
                    }
                });
            }
        }
        catch (e) {
            alert('ShowFullWindow exception：' + e.message + ', ' + e.name);
        }
    },

    // 退出全屏
    HideFullWindow: function (notify) {
        try {
            if (notify && notify.handle) {
                NPCCILY.windowContainers.each(function (item) {
                    var windowContainerStruct = item.value;
                    var winObj = windowContainerStruct.window;
                    if (winObj == notify.handle && winObj.status.playvideoing) {
                        var fullWindow = notify.keyData;
                        NPCCIF.SetRenderWnd(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle, winObj.pwHandle);
                    }
                });

                WebQuery.windowContainers.each(function(item) {
                    var windowContainerStruct = item.value;
                    var winObj = windowContainerStruct.window;
                    if (winObj == notify.handle && winObj.status.playvideoing) {
                        NPCCIF.SetRenderWnd(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle, winObj.pwHandle);
                    }
                });
            }
        }
        catch (e) {
            alert('HideFullWindow exception：' + e.message + ', ' + e.name);
        }
    },

    // 清晰度音量条件控制
    limitInput: function (o){
        o.value = o.value.replace(/[^0-9-]+/,'');
        var value = o.value;
        var min = 1;
        var max = 100;
        if(parseInt(value) < min || parseInt(value) > max){
            alert('请输入1-100之间的数字');
            o.value = '';
        }
    },

    // 点击实时指挥
    previewClick: function() {
        WebPreview.removePlaybackWindow();
        if ($('.resourceTree').css('display') == 'none') {
            $('.resourceTree').css('display', 'block');
            $('.resourceTreeFrond').css('display', 'none');
            WebTree.ConstructResourceTree(WebClient.hServer, WebClient.rootDomainName, $('#resourceTree'), NPCCEnum.resourceTreeTypeDemo.resourceTreeTypeDemo_platform);
        }

        $('div.video').removeClass("videoWnd_selected").addClass("videoWnd_border");
        $('#video11').addClass("videoWnd_selected").removeClass("videoWnd_border");
        var key = $('#video11').attr('id');
        WebClient.setFocus(key);

        var windowContainerStruct = NPCCILY.windowContainers.get('video11');
        windowContainerStruct.focus = true;
        if (windowContainerStruct.window){
            WebPreview.ClickVideoWindow(windowContainerStruct.window);
        }
    },

    // 点击实时指挥和查询回放时若是回放视频未关闭，关闭窗口，再次切换到实时回放模块时，创建窗口，将原来的视频内容重新移入
    removePlaybackWindow: function() {
        var windowContainerStruct = NPCCILY.windowContainers.get('videoPlayback');
        var winObj = windowContainerStruct.window;
        if (winObj && winObj.status.playvideoing) {
            WebPreview.playbackWinObj = winObj;
            WebPreview.StopVideoByWindowObj(windowContainerStruct, NPCCEnum.pauseOrCloseVideoType.pauseOrCloseVideo_Type_Pause);
            NPCCIF.RefreshImage(NPCCILY.Plug.ccPlugin, winObj.params.mediaHandle);
        }
        else {
            $('#videoPlayback_pw').remove();
            delete windowContainerStruct.window;
        }
    },

    // 关闭实时回放的视频
    stopPlaybackVideo: function() {
        if (WebClient.hServer) {
            var windowContainerStruct = NPCCILY.windowContainers.get('videoPlayback');
            if (windowContainerStruct) {
                WebPreview.firstGetRealTime = 0;
                WebPreview.isJumpPreview = false;
                WebPreview.StopVideoByWindowObj(windowContainerStruct);
                // $('#videoPlayback_pw').remove();
                // delete windowContainerStruct.window;
                $('.playbackTime').html(''); // 回放时长
                $('.playbackTimeUTC').html(''); // 回放时间
                $('.realtime').html(''); // 实时预览总时长
                $('.realtimeUTC').html(''); // 实时时间
                $('.startRealtimeUTC').html('');//开始实时时间
                $('.startPlaybackTimeUTC').html('');//开始回放时间
            }
        }
    },

    //手动开启/停止平台录像
    ManualStartOrStopStorage: function() {
        try {
            var windowContainerStruct = WebPreview.GetFocusVideoWindow();
            if (windowContainerStruct && windowContainerStruct.window) {
                if (windowContainerStruct.window.status.playvideoing) {
                    var winObj = windowContainerStruct.window;
                    var resHandle = winObj.params.cameraResHandle;
                    var devHandle = NPCCIF.COMMON_GetParent(NPCCILY.Plug.ccPlugin, resHandle).value;
                    //获取到的ucResIdx是字符串，需要转成整型
                    var ucResIdx = NPCCIF.COMMON_GetResIdx(NPCCILY.Plug.ccPlugin, resHandle).value;

                    if (!winObj.status.playstorage) {
                        var operator = NPCCIF.RSTW_OSS_ManualStartStorage(NPCCILY.Plug.ccPlugin, devHandle,
                            Number(ucResIdx), false, NPCCEnum.StreamType.ST_SubStream, 5);
                        if (operator.errorCode == ErrorCode.NP_E_OK) {
                            winObj.status.playstorage = true;
                            $('.platformStorage').val('手动停止平台录像');
                            console.log('开启平台录像');
                        }
                        else {
                            alert('开启平台录像失败：' + ErrorCode.Error2Desc(operator.errorCode));
                            return false;
                        }
                    }
                    else {
                        var operator = NPCCIF.RSTW_OSS_ManualStopStorage(NPCCILY.Plug.ccPlugin, devHandle,
                            Number(ucResIdx), false, NPCCEnum.StreamType.ST_SubStream);
                        if (operator.errorCode == ErrorCode.NP_E_OK) {
                            winObj.status.playstorage = false;
                            $('.platformStorage').val('手动开启平台录像');
                            console.log('停止平台录像');
                        }
                        else {
                            alert('停止平台录像失败：' + ErrorCode.Error2Desc(operator.errorCode));
                            return false;
                        }
                    }

                    return true;
                }
                else {
                    // alert('请先选择一个视频播放');
                }

            }
        }
        catch (e) {
            alert('手动开启/停止平台录像异常：' + e.name + '::' + e.message);
        }
    },

    // 点击实时回放
    previewPlaybackClick: function () {
        $('.previewPlayback').css('display', 'block');
        if (WebClient.hServer) {
            if (WebPreview.playbackWinObj) {
                WebPreview.createVideoWindow('videoPlayback');
                var windowContainerStruct = NPCCILY.windowContainers.get('videoPlayback');
                windowContainerStruct.window.params = WebPreview.playbackWinObj.params;
                windowContainerStruct.window.status = WebPreview.playbackWinObj.status;
                setTimeout(function () {
                    var operator = NPCCIF.SetRenderWnd(NPCCILY.Plug.ccPlugin, windowContainerStruct.window.params.mediaHandle,
                        windowContainerStruct.window.pwHandle);
                },0)
                WebPreview.playbackWinObj = null;

                WebPreview.ClickVideoWindow(windowContainerStruct.window);
            }
        }

        $('div.video').removeClass("videoWnd_selected").addClass("videoWnd_border");
        $('#videoPlayback').addClass("videoWnd_selected").removeClass("videoWnd_border");
        var key = $('#videoPlayback').attr('id');
        WebClient.setFocus(key);
    },

    // 跳到最新实时预览
    jumpLatestPreview: function() {
        var realtimeTime = NPCCIF.RSTW_GetRealtimeTimestamp(NPCCILY.Plug.ccPlugin, WebPreview.mediaHandle).value;
        var operator = NPCCIF.RSTW_JumpPreview(NPCCILY.Plug.ccPlugin, WebPreview.mediaHandle,  realtimeTime);
        WebPreview.isJumpPreview = false;
        console.log('跳到最新实时预览：' + operator.errorCode);
    },

    // 创建窗口: 视频播放窗口
    createVideoWindow: function(key) {
        var windowContainerStruct = NPCCILY.windowContainers.get(key);
        if (windowContainerStruct) {
            // 创建窗口
            var window = NPCCILY.CreateWindow(WebClient.connectId, windowContainerStruct.container);
            if (window.errorCode == ErrorCode.NP_E_OK) {
                windowContainerStruct.window = window.value;
                var windowEvent = new NPCCStruct.WindowEventStruct();
                // 左键单击事件
                windowEvent.lbtn_click.status = true;
                windowEvent.lbtn_click.callback = function (notify) {
                    WebClient.ReceivePWEvent(notify);
                };
                NPCCILY.WindowAttachEvent.Init(windowContainerStruct.window, windowEvent);

                var windowEvent2 = new NPCCStruct.WindowEventStruct();
                // 全屏窗口显示
                windowEvent2.full_screen_window_show.status = true;
                windowEvent2.full_screen_window_show.callback = function (notify) {
                    WebClient.ReceivePWEvent(notify);
                };
                NPCCILY.WindowAttachEvent.Init(windowContainerStruct.window, windowEvent2);

                var windowEvent3 = new NPCCStruct.WindowEventStruct();
                // 全屏窗口隐藏
                windowEvent3.full_screen_window_hide.status = true;
                windowEvent3.full_screen_window_hide.callback = function (notify) {
                    WebClient.ReceivePWEvent(notify);
                };
                NPCCILY.WindowAttachEvent.Init(windowContainerStruct.window, windowEvent3);
            }
        }
    },

    // 点击即时会话
    chatMsgClick:function () {
        WebPreview.removePlaybackWindow();
    },

    // 登录即时会话服务
    chatLogin: function () {
        if (WebClient.hServer) {
            var operator = NPCCIF.RSTW_Chat_Login(NPCCILY.Plug.ccPlugin, WebClient.hServer);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                alert('登录即时会话服务成功');
            }
        }

    },

    // 退出即时会话服务
    chatLogout: function () {
        if (WebClient.hServer) {
            var operator = NPCCIF.RSTW_Chat_Logout(NPCCILY.Plug.ccPlugin, WebClient.hServer);
            if (operator.errorCode == ErrorCode.NP_E_OK) {
                console.log('退出即时会话服务成功');
                WebClient.chatStatusHasLogin = false;
            }
        }

    },

    // 发送消息
    sendChatMsg: function () {
        if (WebClient.hServer) {
            if (WebClient.chatStatusHasLogin == true) {
                //表示即时会话服务已连接
                // 发送短消息，非阻塞接口，发送通知告知上层是否发送成功
                var operator = NPCCIF.RSTW_Chat_SendShortMsg(NPCCILY.Plug.ccPlugin, WebClient.hServer,
                    NPCCUtils.encodeUnicode(NPCCUtils.UnicodetoUTF8($('#content').val())),
                    NPCCUtils.encodeUnicode(NPCCUtils.UnicodetoUTF8($('#sendUserName').val())));
                if (operator.errorCode == ErrorCode.NP_E_OK) {
                    console.log('发送短消息成功');
                }

            }
            else {
                console.log('即时会话服务未连接');
            }

        }

    },
}


// 进度条
$( "a.videoPreview_progress" ).slider({
    value: 0,
    orientation: "horizontal",
    min: 0,
    max: 100,
    range: "min",
    animate: true,
    stop: function( event, ui) {
        // 跳到指定位置继续播放
        var jumpVlaue = String(ui.value * 1000 + Number(WebPreview.firstGetRealTime));
        // console.log('跳转的位置：' + ui.value);
        var operator = NPCCIF.RSTW_JumpPreview(NPCCILY.Plug.ccPlugin, WebPreview.mediaHandle, jumpVlaue);
        if (operator.errorCode == ErrorCode.NP_E_OK) {
            WebPreview.startPlaybackTimeUTC = true;
            var realtime =  $('.realtime').attr("href");
            if (ui.value > realtime) {
                WebPreview.isJumpPreview = false;
            }
            else if (ui.value < realtime && !WebPreview.isJumpPreview) {
                WebPreview.isJumpPreview = true;
            }
            console.log('跳转成功');
        }
        else {
            console.log('跳转失败：' + ErrorCode.Error2Desc(operator.errorCode));
        }

    },

});